package org.binglan.youziyuepai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.binglan.youziyuepai.entity.Image;
import org.binglan.youziyuepai.mapper.ImageMapper;
import org.binglan.youziyuepai.service.ImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.n3r.idworker.Sid;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.awt.image.ImagingOpException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 图片表 服务实现类
 * </p>
 *
 * @author sanjin
 * @since 2019-04-09
 */
@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements ImageService {

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public List<Image> saveImageBatch(List<Image> imageObjs) {
        imageObjs.forEach(image -> {
            image.setId(Sid.nextShort());
        });
        saveBatch(imageObjs);
        return imageObjs;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Image queryImageById(String imageId) {
        return getById(imageId);
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Boolean isExistsIds(List<String> imageIds) {
        QueryWrapper<Image> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",imageIds);
        return imageIds.size() == this.count(queryWrapper);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Image> queryImagesByActivityId(Integer activityId) {
        return baseMapper.queryImageByActivityId(activityId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Image> queryImagesByImageIds(List<Integer> imageIds) {
        Collection<Image> images = this.listByIds(imageIds);
        List<Image> imageList = images.stream().map(image -> {
            return image;
        }).collect(Collectors.toList());
        return imageList;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Image> queryImageByPersonalDynamicId(String personalDynamicId) {
        if (personalDynamicId == null) return null;
        return baseMapper.queryImageByPersonalDynamicId(personalDynamicId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Image saveImage(Image image) {
        image.setId(Sid.nextShort());
        boolean save = this.save(image);
        return image;
    }

}
